home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Sample Code / Sprocket / Lib / Window.cp < prev   
Encoding:
Text File  |  1994-12-06  |  22.4 KB  |  911 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Window.cp
  3.  
  4.     Contains:    Implementation of TWindow, a base class which provides a
  5.                 framework for building way-cool windows which even John
  6.                 Sullivan would be happy with. Floating windows and “smart
  7.                 zooming” algorithms are based on code samples provided by
  8.                 Dean Yu. Tim Craycroft, the guy making the window manager
  9.                 do all this work for you has also been a great help.
  10.                 
  11.     Written by: Dave Falkenburg
  12.  
  13.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  14.  
  15.     Change History (most recent first):
  16.     
  17.         <10>     12/6/94    DRF        Rolled in David Den Boer’s fixes. Also add the conditionals for
  18.                                     newest universal headers again.
  19.          <9>    11/23/94    DRF        Bite the bullet just require the latest universal headers
  20.          <8>    11/17/94    DRF        Add casts for CFront & PPCC. Also dealt with the change to
  21.                                     ClipAbove in the latest universal headers.
  22.          <7>    11/12/94    DRF        Added AdjustMenusBeforeMenuSelection.
  23.          <6>     11/8/94    DRF        Add some better menu handling methods.
  24.          <5>     9/27/94    DRF         AppLib.h is now Sprocket.h
  25.          <4>      9/9/94    DRF        Reorganized headers and removed redundant #includes.
  26.          <3>      9/4/94    DRF        Added DrawJustTheGrowIcon.
  27.          <2>     8/27/94    DRF        In TWindow::Close, call window’s (de)Activate method before
  28.                                     closing so that menus can be properly updated.
  29.     
  30.     To Do:        Make sure invisible windows can be created & managed
  31.                 Handle modal windows as another class of windows
  32.                 Fix activate bugs when showing and hiding windows
  33.                 Window positioning methods (getters and setters)
  34.                 Display Manager support
  35.                 Changes to support AEObject model
  36.  */
  37.  
  38. #include "Sprocket.h"
  39. #include "Window.h"
  40.  
  41. #include <Script.h>        //    for GetMBarHeight()
  42. #include <LowMem.h>        //    for LMGetWindowList()
  43.  
  44.  
  45. const short            kFloatingWindowKind        = 1000;
  46. const short            kNormalWindowKind        = 1001;
  47. const WindowPtr     kNoFloatingWindows        = (WindowPtr) -1;
  48.  
  49. const short            kScreenEdgeSlop            = 4;
  50. const short            kSpaceForFinderIcons    = 64;
  51. const short            kMinimumTitleBarHeight    = 21;
  52. const short            kMinimumWindowSize        = 32;
  53.  
  54. static void            HiliteShowHideFloatingWindows(Boolean hiliting,Boolean hiding);
  55.  
  56. static void            FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect, GDHandle * theBestDevice);
  57. static pascal void    CalculateWindowAreaOnDevice(short depth,short deviceFlags,GDHandle targetDevice,long userData);
  58.  
  59.  
  60. struct    CalcWindowAreaDeviceLoopUserData
  61.     {
  62.     GDHandle    fScreenWithLargestPartOfWindow;
  63.     long        fLargestArea;
  64.     Rect        fWindowBounds;
  65.     };
  66.  
  67.  
  68.  
  69.  
  70.  
  71. TWindow::TWindow()
  72.     {
  73.     }
  74.  
  75.  
  76. TWindow::~TWindow()
  77.     {
  78.     }
  79.  
  80.  
  81. void
  82. TWindow::CreateWindow(WindowType typeOfWindowToCreate /* = kNormalWindow */)
  83.     {
  84.     WindowPtr    behindWindow,oldFrontMostWindow;
  85.     
  86.     if (typeOfWindowToCreate == kModalWindow)
  87.         {
  88.         DebugStr((StringPtr) "\pModal windows aren’t supported yet");
  89.         fWindowType = kFloatingWindow;
  90.         return;
  91.         }
  92.     else if (typeOfWindowToCreate == kFloatingWindow)
  93.         {
  94.         behindWindow = (WindowPtr) -1;
  95.         oldFrontMostWindow = FrontWindow();
  96.  
  97.         fWindowType = kFloatingWindow;
  98.         }
  99.     else if (typeOfWindowToCreate == kNormalWindow)
  100.         {
  101.         behindWindow = LastFloatingWindow();
  102.  
  103.         fWindowType = kNormalWindow;
  104.         
  105.         if (behindWindow == kNoFloatingWindows)
  106.             oldFrontMostWindow = FrontNonFloatingWindow();    // •••
  107.         else
  108.             oldFrontMostWindow = (WindowPtr) ((WindowPeek) behindWindow)->nextWindow;
  109.         }
  110.  
  111.     fWindow = this->MakeNewWindow(behindWindow);
  112.     fIsVisible = ((WindowPeek) fWindow)->visible;
  113.  
  114.     if (fWindow)
  115.         {
  116.         SetWRefCon(fWindow,(long) this);
  117.  
  118.         if (typeOfWindowToCreate == kModalWindow)
  119.             {
  120.             DebugStr((StringPtr) "\pCan’t create Modal windows yet");
  121.             }
  122.         else if (typeOfWindowToCreate == kFloatingWindow)
  123.             {
  124.             ((WindowPeek) fWindow)->windowKind = kFloatingWindowKind;
  125.             
  126.             //    make sure the other window stays hilited
  127.             if (oldFrontMostWindow)
  128.                 HiliteAndActivateWindow(oldFrontMostWindow,true);
  129.             }
  130.         else if (typeOfWindowToCreate == kNormalWindow)
  131.             {
  132.             ((WindowPeek) fWindow)->windowKind = kNormalWindowKind;
  133.  
  134.             //    unhighlight the old front window
  135.             if (oldFrontMostWindow)
  136.                 HiliteAndActivateWindow(oldFrontMostWindow,false);
  137.  
  138.             //    hilite the new window…
  139.             HiliteAndActivateWindow(fWindow,true);
  140.             }
  141.         }
  142.     }
  143.  
  144.  
  145. void
  146. TWindow::AdjustCursor(EventRecord * /* anEvent */)
  147.     {
  148.     }
  149.  
  150. void
  151. TWindow::Idle(EventRecord * /* anEvent */)
  152.     {
  153.     }
  154.     
  155. void
  156. TWindow::Activate(Boolean /* activating */)
  157.     {
  158.     }
  159.     
  160. void
  161. TWindow::Draw(void)
  162.     {
  163.     }
  164.     
  165. void
  166. TWindow::Click(EventRecord * /* anEvent */)
  167.     {
  168.     }
  169.     
  170. void
  171. TWindow::KeyDown(EventRecord * /* anEvent */)
  172.     {
  173.     }
  174.  
  175.  
  176. void
  177. TWindow::Select(void)
  178.     {
  179.     WindowPtr    currentFrontWindow;
  180.     
  181.     if (fWindowType == kFloatingWindow)
  182.         currentFrontWindow = FrontWindow();
  183.     else if (fWindowType == kNormalWindow)
  184.         currentFrontWindow = FrontNonFloatingWindow();
  185.     else
  186.         {
  187.         }
  188.  
  189.     if (currentFrontWindow != fWindow)
  190.         {
  191.         if (fWindowType == kFloatingWindow)
  192.             BringToFront(fWindow);
  193.         else
  194.             {
  195.             WindowPtr    lastFloater = LastFloatingWindow();
  196.  
  197.             // Deactivate the window currently in front.
  198.  
  199.             HiliteAndActivateWindow(currentFrontWindow,false);
  200.  
  201.             if (lastFloater == kNoFloatingWindows)
  202.                 {
  203.                 //    If there are no floating windows,
  204.                 //    just call SelectWindow like the good ol’ days
  205.  
  206.                 SelectWindow(fWindow);
  207.                 }
  208.             else
  209.                 {
  210.                 // Bring it behind the last floating window and activate it.
  211.                 // Note that Inside Mac 1 states that you need to call PaintOne() and CalcVis() on a
  212.                 // window if you are using SendBehind() to bring it closer to the front.  With System 7,
  213.                 // this is no longer necessary.
  214.  
  215.                 SendBehind(fWindow,lastFloater);
  216.                 }
  217.  
  218.             //    Hilite the new front window
  219.  
  220.             HiliteAndActivateWindow(fWindow,true);
  221.             }
  222.         }
  223.     }
  224.  
  225.  
  226. void
  227. TWindow::Drag(Point startPoint)
  228.     {
  229.     GrafPtr        savePort;
  230.     KeyMap        theKeyMap;
  231.     Boolean        commandKeyDown = false;
  232.     RgnHandle    draggingRegion;
  233.     long        dragResult;
  234.     
  235.     if (WaitMouseUp())        //    de-bounce?
  236.         {
  237.         // Set up the Window Manager port.
  238.     
  239.         GetPort(&savePort);
  240.         SetPort(gWindowManagerPort);
  241.         SetClip(GetGrayRgn());
  242.  
  243.         // Check to see if the command key is down.
  244.     
  245.         GetKeys(theKeyMap);
  246.         commandKeyDown = ((theKeyMap[1] & 0x8000) != 0);
  247.         
  248.         if (commandKeyDown)
  249.             {
  250.             //    We’re not going to change window ordering,
  251.             //    so make sure that we don’t drag in front of
  252.             //    other windows which may be in front of ours.
  253.  
  254. #if    qUseNewUniversalInterfaces
  255.             ClipAbove(fWindow);
  256. #else
  257.             ClipAbove((WindowPeek) fWindow);
  258. #endif
  259.             }
  260.         else if (fWindowType != kFloatingWindow)
  261.             {
  262.             //    We’re dragging a normal window, so make sure
  263.             //    that we don’t drag in front of any floating
  264.             //    windows.
  265.  
  266. #if    qUseNewUniversalInterfaces
  267.             ClipAbove(FrontNonFloatingWindow());
  268. #else
  269.             ClipAbove((WindowPeek) FrontNonFloatingWindow());
  270. #endif
  271.             }
  272.         
  273.         //    Drag an outline of the window around the desktop.
  274.         //    NOTE: DragGrayRgn destroys the region passed in, so make a copy
  275.  
  276.         draggingRegion = NewRgn();
  277.         CopyRgn(((WindowPeek)fWindow)->strucRgn,draggingRegion);
  278.         dragResult = DragGrayRgn(draggingRegion, startPoint, &gDeskRectangle, &gDeskRectangle, noConstraint, nil);
  279.         DisposeRgn(draggingRegion);
  280.  
  281.     
  282.         SetPort(savePort);    //    Get back to old port
  283.  
  284.         if ((dragResult != 0) && (dragResult != 0x80008000))
  285.             {
  286.             this->Nudge((short) (dragResult & 0xFFFF),(short) (dragResult >> 16));
  287.             }
  288.         }
  289.  
  290.     if (!commandKeyDown)
  291.         Select();
  292.     }
  293.  
  294. void
  295. TWindow::Nudge(short horizontalDistance, short verticalDistance)
  296.     {
  297.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  298.     short        newHorizontalPosition,newVerticalPosition;
  299.     
  300.     newHorizontalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.left + horizontalDistance;
  301.     newVerticalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.top + verticalDistance;
  302.  
  303.     MoveWindow(fWindow,newHorizontalPosition,newVerticalPosition,false);
  304.     }
  305.  
  306. void
  307. TWindow::Grow(Point startPoint)
  308.     {
  309.     GrafPtr    oldPort;
  310.     long    newSize;
  311.     Rect    oldWindowRect,resizeLimits;
  312.     
  313.     GetPort(&oldPort);
  314.     
  315.     GetWindowSizeLimits(&resizeLimits);
  316.     newSize = GrowWindow(fWindow,startPoint,&resizeLimits);
  317.     if (newSize)
  318.         {
  319.         oldWindowRect = fWindow->portRect;
  320.         SizeWindow(fWindow,(short) newSize,(short) (newSize >> 16),true);
  321.         SetPort(fWindow);
  322.         this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  323.         }
  324.     
  325.     SetPort(oldPort);
  326.     }
  327.  
  328.  
  329. void
  330. TWindow::Zoom(short zoomState)
  331.     {
  332.     GrafPtr        oldPort;
  333.     FontInfo    systemFontInfo;
  334.     short        titleBarHeight;
  335.     Rect        bestScreenRect,perfectWindowRect,scratchRect;
  336.     short        amountOffscreen;
  337.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  338.     GDHandle    bestDevice;
  339.     
  340.     GetPort(&oldPort);
  341.  
  342.     //    Figure out the height of the title bar so we can properly position
  343.     //    a window. The algorithm is stolen from the System 7.x 'WDEF' (0)
  344.     //
  345.     //    This probably isn’t the best thing to do: A better way might be 
  346.     //    to diff the structure and content region rectangles?
  347.  
  348.     SetPort(gWindowManagerPort);
  349.     GetFontInfo(&systemFontInfo);
  350.     titleBarHeight = (short) (systemFontInfo.ascent + systemFontInfo.descent + 4);
  351.     if ((titleBarHeight % 2) == 1)
  352.         titleBarHeight--;
  353.     if (titleBarHeight < kMinimumTitleBarHeight)
  354.         titleBarHeight = kMinimumTitleBarHeight;
  355.  
  356.  
  357.     //    Only do the voodoo magic if we are really “zooming” the window.
  358.  
  359.     if (zoomState == inZoomOut)
  360.         {
  361.         FindScreenRectWithLargestPartOfWindow(fWindow,&bestScreenRect,&bestDevice);
  362.         bestScreenRect.top += titleBarHeight;
  363.  
  364.         this->GetPerfectWindowSize(&perfectWindowRect);
  365.         OffsetRect(&perfectWindowRect,-perfectWindowRect.left,-perfectWindowRect.top);
  366.  
  367.         //    Take the zero-pined perfect window size and move it to
  368.         //    the top left of the    window’s content region.
  369.  
  370.         OffsetRect(&perfectWindowRect,(**windowAsWindowPeek->contRgn).rgnBBox.left,
  371.                                       (**windowAsWindowPeek->contRgn).rgnBBox.top);
  372.  
  373.         
  374.         //    Does perfectWindowRect fit completely on the best screen?
  375.         
  376.         SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  377.         if (!EqualRect(&perfectWindowRect, &scratchRect))
  378.             {
  379.             //    SectRect sez perfectWindowRect doesn’t completely fit
  380.             //    on the screen, so bump the window so that more of it fits.
  381.  
  382.             //    Make sure that the left edge of perfectWindowRect is forced
  383.             //    onto the best screen.  This is in case we are bumping
  384.             //    the window to the right.
  385.  
  386.             amountOffscreen = bestScreenRect.left - perfectWindowRect.left;
  387.             if (amountOffscreen > 0)
  388.                 {
  389.                 perfectWindowRect.left += amountOffscreen;
  390.                 perfectWindowRect.right += amountOffscreen;
  391.                 }
  392.  
  393.             //    Make sure that the left edge of perfectWindowRect is forced
  394.             //    onto the best screen.  This is in case we are bumping
  395.             //    the window downward to a new screen.
  396.     
  397.             amountOffscreen = bestScreenRect.top - perfectWindowRect.top;
  398.             if (amountOffscreen > 0)
  399.                 {
  400.                 perfectWindowRect.top += amountOffscreen;
  401.                 perfectWindowRect.bottom += amountOffscreen;
  402.                 }
  403.  
  404.             //    If right edge of window falls off the screen,
  405.             //        Move window to the left until the right edge IS on the screen
  406.             //        OR the left edge is at bestScreenRect.left
  407.  
  408.             amountOffscreen = perfectWindowRect.right - bestScreenRect.right;
  409.             if (amountOffscreen > 0)
  410.                 {
  411.                 //    Are we going to push the left edge offscreen? If so, change the
  412.                 //    offset so we move the window all the way over to the left.
  413.                 
  414.                 if ((perfectWindowRect.left - amountOffscreen) < bestScreenRect.left)
  415.                     amountOffscreen = perfectWindowRect.left - bestScreenRect.left;
  416.  
  417.                 perfectWindowRect.left -= amountOffscreen;
  418.                 perfectWindowRect.right -= amountOffscreen;
  419.                 }
  420.  
  421.             //    If bottom edge of window falls off the screen,
  422.             //        Move window to up until the bottom edge IS on the screen
  423.             //        OR the top edge is at bestScreenRect.top
  424.  
  425.             amountOffscreen = perfectWindowRect.bottom - bestScreenRect.bottom;
  426.             if (amountOffscreen > 0)
  427.                 {
  428.                 //    Are we going to push the top edge offscreen? If so, change the
  429.                 //    offset so we move the window just to the top.
  430.                 
  431.                 if ((perfectWindowRect.top - amountOffscreen) < bestScreenRect.top)
  432.                     amountOffscreen = perfectWindowRect.top - bestScreenRect.top;
  433.  
  434.                 perfectWindowRect.top -= amountOffscreen;
  435.                 perfectWindowRect.bottom -= amountOffscreen;
  436.                 }
  437.  
  438.             SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  439.             if (!EqualRect(&perfectWindowRect, &scratchRect))
  440.                 {
  441.                 //    The edges of the window still fall offscreen,
  442.                 //    so make the window smaller until it fits.
  443.                 
  444.                 if (perfectWindowRect.bottom > bestScreenRect.bottom)
  445.                     perfectWindowRect.bottom = bestScreenRect.bottom;
  446.  
  447.                 //    If the right edge is still falling off,
  448.                 //        save space for Finder’s disk icons as well.
  449.  
  450.                 if (perfectWindowRect.right > bestScreenRect.right)
  451.                     {
  452.                     perfectWindowRect.right = bestScreenRect.right;
  453.                     
  454.                     //    If we were on the main screen, leave room for Finder icons, too.
  455.                     
  456.                     if (bestDevice == GetMainDevice())
  457.                         perfectWindowRect.right -= kSpaceForFinderIcons;
  458.                     }
  459.                 }
  460.             }
  461.  
  462.         //    Stash our new rectangle inside of the Window’s dataHandle
  463.         //    so that ZoomWindow does the right thing.
  464.         
  465.         (**((WStateDataHandle) (windowAsWindowPeek->dataHandle))).stdState = perfectWindowRect;
  466.         }
  467.  
  468.     //    HEY YOU! Don’t forget to set the port to the window being zoomed
  469.     //    Why, you ask? Because IM-IV-50 says to; otherwise you die
  470.     
  471.     SetPort(fWindow);
  472.  
  473.     Rect    oldWindowRect = fWindow->portRect;
  474.     
  475.     ZoomWindow(fWindow,zoomState,false);
  476.     this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  477.  
  478.     SetPort(oldPort);
  479.     }
  480.  
  481. void
  482. TWindow::ShowHide(Boolean showFlag)
  483.     {
  484.     //    Here we need the “::” in front of ShowHide to indicate we are calling
  485.     //    the global function, and not the method ShowHide. Unintended recursion
  486.     //    can do bad things to the unsuspecting programmer.
  487.     
  488.     //    Some C++ programmers would always prepend the “::” on function calls.
  489.     
  490.     ::ShowHide(fWindow,showFlag);
  491.     fIsVisible = showFlag;
  492.     }
  493.     
  494.  
  495. Boolean
  496. TWindow::EventFilter(EventRecord * /* theEvent */)
  497.     {
  498.     return false;
  499.     }
  500.     
  501.  
  502. void
  503. TWindow::GetPerfectWindowSize(Rect *perfectSize)
  504.     {
  505.     *perfectSize = qd.screenBits.bounds;
  506.     }
  507.  
  508. void
  509. TWindow::GetWindowSizeLimits(Rect *limits)
  510.     {
  511.     limits->top = limits->left = kMinimumWindowSize;
  512.     limits->right = gDeskRectangle.right - gDeskRectangle.left;
  513.     limits->bottom = gDeskRectangle.bottom - gDeskRectangle.top;
  514.     }
  515.  
  516. void
  517. TWindow::AdjustForNewWindowSize(Rect * /* oldRect */, Rect * /* newSize */)
  518.     {
  519.     }
  520.  
  521.  
  522. Boolean
  523. TWindow::IsVisible(void)
  524.     {
  525.     return fIsVisible;
  526.     }
  527.  
  528.  
  529. Boolean
  530. TWindow::CanClose(void)
  531.     {
  532.     return true;
  533.     }
  534.  
  535.  
  536. Boolean
  537. TWindow::Close(void)
  538.     {
  539.     WindowPtr    newFrontWindow = nil;
  540.     
  541.     if (FrontNonFloatingWindow() == fWindow)
  542.         newFrontWindow = (WindowPtr) ((WindowPeek) fWindow)->nextWindow;
  543.  
  544.     this->Activate(false);
  545.     DisposeWindow(fWindow);
  546.  
  547.     if (newFrontWindow)
  548.         HiliteAndActivateWindow(newFrontWindow,true);
  549.  
  550.     return true;
  551.     }
  552.  
  553.  
  554. Boolean
  555. TWindow::DeleteAfterClose(void)
  556.     {
  557.     return true;
  558.     }
  559.  
  560.  
  561. void
  562. TWindow::AdjustMenusBeforeMenuSelection(void)
  563.     {
  564.     }
  565.  
  566.     
  567. void
  568. TWindow::DoMenuSelection(short /* menu */, short /* item */)
  569.     {
  570.     }
  571.     
  572.  
  573. void
  574. TWindow::DoMenuCommand(unsigned long /* menuCommand */)
  575.     {
  576.     }
  577.  
  578.  
  579. OSErr
  580. TWindow::HandleDrag(DragTrackingMessage dragMessage,DragReference theDrag)
  581.     {
  582.     OSErr    result = dragNotAcceptedErr;
  583.     
  584.     switch (dragMessage)
  585.         {
  586.         case    dragTrackingEnterWindow:
  587.             result = this->DragEnterWindow(theDrag);
  588.             break;
  589.         
  590.         case    dragTrackingInWindow:
  591.             result = this->DragInWindow(theDrag);
  592.             break;
  593.             
  594.         case    dragTrackingLeaveWindow:
  595.             result = this->DragLeaveWindow(theDrag);
  596.             break;
  597.             
  598.         default:
  599.             break;
  600.         }
  601.  
  602.     return result;
  603.     }
  604.  
  605.  
  606. OSErr
  607. TWindow::DragEnterWindow(DragReference /* theDrag */)
  608.     {
  609.     return dragNotAcceptedErr;
  610.     }
  611.  
  612.  
  613. OSErr
  614. TWindow::DragInWindow(DragReference /* theDrag */)
  615.     {
  616.     return dragNotAcceptedErr;
  617.     }
  618.  
  619.  
  620. OSErr
  621. TWindow::DragLeaveWindow(DragReference /* theDrag */)
  622.     {
  623.     return dragNotAcceptedErr;
  624.     }
  625.     
  626.  
  627. OSErr
  628. TWindow::HandleDrop(DragReference /* theDrag */)
  629.     {
  630.     return dragNotAcceptedErr;
  631.     }
  632.  
  633.  
  634. ///////////////////////////////////////////////////////////////////////////
  635. //
  636. //    Utility Functions used for floating windows
  637. //
  638.  
  639. TWindow *
  640. GetWindowObject(WindowPtr aWindow)
  641.     {
  642.     short    wKind;
  643.     
  644.     if (aWindow != nil)
  645.         {
  646.         wKind = ((WindowPeek) aWindow)->windowKind;
  647.  
  648.         if (wKind >= userKind)
  649.             {
  650.             //    All windowKinds >= userKind are based upon TWindow
  651.  
  652.             return (TWindow *) GetWRefCon(aWindow);
  653.             }
  654.         }
  655.     return (TWindow *) nil;
  656.     }
  657.  
  658.  
  659. ////////////////////////////////////////////////////////////////////////
  660. //
  661. //    Utility functions
  662.  
  663.  
  664. pascal WindowPtr
  665. GetNewColorOrBlackAndWhiteWindow(short windowID, void *wStorage, WindowPtr behind)
  666.     {
  667.     if (gHasColorQuickdraw)
  668.         return GetNewCWindow(windowID,wStorage,behind);
  669.     else
  670.         return GetNewWindow(windowID,wStorage,behind);
  671.     }
  672.  
  673.  
  674. pascal WindowPtr
  675. NewColorOrBlackAndWhiteWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title, Boolean visible, short theProc, WindowPtr behind, Boolean goAwayFlag, long refCon)
  676.     {
  677.     if (gHasColorQuickdraw)
  678.         return NewCWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  679.     else
  680.         return NewWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  681.     }
  682.  
  683.  
  684. void
  685. DrawJustTheGrowIcon(WindowPtr aWindow)
  686.     {
  687.     GrafPtr        savedPort;
  688.     RgnHandle    savedClip = NewRgn();
  689.     Rect        growBoxRect;
  690.     
  691.     GetPort(&savedPort);
  692.     SetPort(aWindow);
  693.     GetClip(savedClip);
  694.  
  695.     //    clip to just the bottom right corner of the window
  696.     
  697.     growBoxRect.top = aWindow->portRect.bottom - kScrollbarWidth;
  698.     growBoxRect.bottom = aWindow->portRect.bottom;
  699.     growBoxRect.left = aWindow->portRect.right - kScrollbarWidth;
  700.     growBoxRect.right = aWindow->portRect.right;
  701.     ClipRect(&growBoxRect);
  702.  
  703.     DrawGrowIcon(aWindow);
  704.  
  705.     SetClip(savedClip);
  706.     DisposeRgn(savedClip);    
  707.  
  708.     SetPort(savedPort);
  709.     }
  710.     
  711.  
  712. WindowPtr
  713. LastFloatingWindow(void)
  714.     {
  715.     WindowPeek    aWindow = (WindowPeek) FrontWindow();
  716.     WindowPtr    lastFloater = (WindowPtr) kNoFloatingWindows;
  717.     
  718.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  719.         {
  720.         if (aWindow->visible)
  721.             lastFloater = (WindowPtr) aWindow;
  722.  
  723.         aWindow = (WindowPeek) aWindow->nextWindow;
  724.         }
  725.     return(lastFloater);
  726.     }
  727.  
  728.  
  729. WindowPtr
  730. FrontNonFloatingWindow(void)
  731.     {
  732.     WindowPeek    aWindow = (WindowPeek) LMGetWindowList();
  733.  
  734.     //    Skip over floating windows
  735.         
  736.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  737.         aWindow = (WindowPeek) aWindow->nextWindow;
  738.  
  739.     //    Skip over invisible, but otherwise normal windows
  740.     
  741.     while (aWindow && (aWindow->visible == 0))
  742.         aWindow = (WindowPeek) aWindow->nextWindow;
  743.         
  744.     return (WindowPtr) aWindow;
  745.     }
  746.  
  747.  
  748. void
  749. HiliteAndActivateWindow(WindowPtr aWindow,Boolean active)
  750.     {
  751.     GrafPtr        oldPort;
  752.     TWindow    *    wobj = GetWindowObject(aWindow);
  753.     
  754.     if (aWindow)
  755.         {
  756.         HiliteWindow(aWindow,active);
  757.  
  758.         if (wobj != nil)
  759.             {
  760.             GetPort(&oldPort);
  761.             SetPort(aWindow);
  762.             wobj->Activate(active);
  763.             SetPort(oldPort);
  764.             }    
  765.         }
  766.     }
  767.  
  768. void
  769. SuspendResumeWindows(Boolean resuming)
  770.     {
  771.     //    When we suspend/resume, hide/show all the visible floaters
  772.     
  773.     HiliteShowHideFloatingWindows(resuming,true);
  774.     }
  775.  
  776. void
  777. HiliteWindowsForModalDialog(Boolean hiliting)
  778.     {
  779.     //    When we display a modal dialog, we need to unhighlight
  780.     //    all visible floaters. We also need to re-hilite them
  781.     //    afterwards.
  782.     
  783.     HiliteShowHideFloatingWindows(hiliting,false);
  784.     }
  785.  
  786. void
  787. HiliteShowHideFloatingWindows(Boolean hiliting,Boolean dohiding)
  788.     {
  789.     WindowPeek    aWindow;
  790.     TWindow *    wobj;
  791.     
  792.     HiliteAndActivateWindow(FrontNonFloatingWindow(),hiliting);
  793.  
  794.     aWindow = (WindowPeek) LMGetWindowList();
  795.     while (aWindow && aWindow->windowKind == kFloatingWindowKind)
  796.         {
  797.         wobj = GetWindowObject((WindowPtr) aWindow);
  798.         
  799.         //    If we are hiding or showing, only hide/show windows
  800.         //    that were visible to begin with.
  801.         
  802.         //    NOTE:    We use our copy of the visible flag so we can
  803.         //            automatically show floaters on a resume event.
  804.         
  805.         //    NOTE:    Since this isn’t a method of TWindow, we don’t
  806.         //            really need the “::” on ShowHide, but as long
  807.         //            as we’re trying to avoid ambiguity.
  808.         
  809.         if (dohiding && (wobj != nil) && (wobj->IsVisible()))
  810.             ::ShowHide((WindowPtr) aWindow,hiliting);
  811.             
  812.         //    All floaters are hilited if any floater is hilited
  813.  
  814.         HiliteWindow((WindowPtr) aWindow,hiliting);
  815.         aWindow = (WindowPeek) aWindow->nextWindow;
  816.         }
  817.     }
  818.  
  819.  
  820. ///////////////////////////////////////////////////////////////////////////
  821. //
  822. //    Routines used for dealing with windows and multiple screens
  823. //
  824.  
  825. pascal void
  826. CalculateWindowAreaOnDevice(short /* depth */,short /* deviceFlags */,GDHandle targetDevice,long userData)
  827.     {
  828.     CalcWindowAreaDeviceLoopUserData *    deviceLoopDataPtr;
  829.     long                                windowAreaOnThisScreen;
  830.     Rect                                windowRectOnThisScreen;
  831.     
  832.     deviceLoopDataPtr = (CalcWindowAreaDeviceLoopUserData *) userData;
  833.  
  834.     SectRect(&deviceLoopDataPtr->fWindowBounds, &(**targetDevice).gdRect,&windowRectOnThisScreen);
  835.     OffsetRect(&windowRectOnThisScreen,-windowRectOnThisScreen.left,-windowRectOnThisScreen.top);
  836.     windowAreaOnThisScreen = windowRectOnThisScreen.right * windowRectOnThisScreen.bottom;
  837.  
  838.     if (windowAreaOnThisScreen > deviceLoopDataPtr->fLargestArea)
  839.         {
  840.         deviceLoopDataPtr->fLargestArea = windowAreaOnThisScreen;
  841.         deviceLoopDataPtr->fScreenWithLargestPartOfWindow = targetDevice;
  842.         }
  843.     }
  844.  
  845.  
  846. DeviceLoopDrawingUPP CallCalcWindowAreaOnDevice = NewDeviceLoopDrawingProc(&CalculateWindowAreaOnDevice);
  847.  
  848.  
  849. void
  850. FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect,GDHandle * theBestDevice)
  851.     {
  852.     RgnHandle                            copyOfWindowStrucRgn;
  853.     CalcWindowAreaDeviceLoopUserData    deviceLoopData;
  854.  
  855.     //    Use DeviceLoop to find out what GDevice contains the largest
  856.     //    portion of the supplied window.
  857.     //
  858.     //    NOTE:    Assumes thePort == the Window Manager Port because we using
  859.     //            the window strucRgn, not contRgn.
  860.  
  861.     deviceLoopData.fScreenWithLargestPartOfWindow = nil;
  862.     deviceLoopData.fLargestArea = -1;
  863.     deviceLoopData.fWindowBounds = (**(((WindowPeek) aWindow)->contRgn)).rgnBBox;
  864.     
  865.     copyOfWindowStrucRgn = NewRgn();
  866.     CopyRgn(((WindowPeek) aWindow)->strucRgn,copyOfWindowStrucRgn);
  867.  
  868.     DeviceLoop(copyOfWindowStrucRgn,CallCalcWindowAreaOnDevice,(long) &deviceLoopData,singleDevices);    
  869.  
  870.     DisposeRgn(copyOfWindowStrucRgn);
  871.     
  872.     *theBestDevice = deviceLoopData.fScreenWithLargestPartOfWindow;
  873.     *theBestScreenRect = (**(deviceLoopData.fScreenWithLargestPartOfWindow)).gdRect;
  874.  
  875.     //    Leave some space around the edges of the screen so window look good, AND
  876.     //    if the best device is the main screen, leave space for the Menubar
  877.     
  878.     InsetRect(theBestScreenRect,kScreenEdgeSlop,kScreenEdgeSlop);
  879.     if (GetMainDevice() == deviceLoopData.fScreenWithLargestPartOfWindow)
  880.         theBestScreenRect->top += GetMBarHeight();
  881.     }
  882.  
  883.  
  884. ///////////////////////////////////////////////////////////////////////////
  885. //
  886. //    Drag Manager callback routines which dispatch to a window’s method
  887. //
  888.  
  889. pascal OSErr
  890. CallWindowDragTrackingHandler(DragTrackingMessage dragMessage,WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  891.     {
  892.     TWindow *wobj = GetWindowObject(theWindow);
  893.     
  894.     if (wobj)
  895.         return(wobj->HandleDrag(dragMessage,theDrag));
  896.     else
  897.         return dragNotAcceptedErr;
  898.     }
  899.  
  900.     
  901. pascal OSErr
  902. CallWindowDragReceiveHandler(WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  903.     {
  904.     TWindow *wobj = GetWindowObject(theWindow);
  905.     
  906.     if (wobj)
  907.         return(wobj->HandleDrop(theDrag));
  908.     else
  909.         return dragNotAcceptedErr;
  910.     }
  911.